<?php

namespace core;

use core\data\MysqlDriver;
use application\webapp\config\MainConfig;

abstract class CoreModel extends MysqlDriver {

    public $table = null;
    public $sql = null;

    public function __construct($table = null) {
        $this->table = $table;
        parent::__construct();
        $this->getConnect(MainConfig::getDb());
    }

    public function find($where = '1=1', $col = '*') {
        $this->sql = 'select ' . $col . ' from ' . $this->table . ' where ' . $where;
        return parent::find($this->sql);
    }

    public function findBy($where = '1=?', $params = array('1'), $col = '*') {
        $this->sql = 'select ' . $col . ' from ' . $this->table . ' where ' . $where;
        $string = explode('?', $this->sql);
        $this->sql = "";
        for ($i = 0; $i < count($params); $i++) {
            $this->sql .= $string[$i] . "'" . $params[$i] . "'";
        }
        return parent::find($this->sql);
    }

    public function findByOne($where = '1=?', $params = array('1'), $col = '*') {
        $this->sql = 'select ' . $col . ' from ' . $this->table . ' where ' . $where;
        $string = explode('?', $this->sql);
        $this->sql = "";
        for ($i = 0; $i < count($params); $i++) {
            $this->sql .= $string[$i] . "'" . $params[$i] . "'";
        }
        parent::find($this->sql);
        if (empty($this->data) || $this->count() == 0) {
            return $this->data;
        } else
            return $this->data = $this->data[0];
    }

    public function findOne($where = '1=1', $col = '*') {
        $this->sql = 'select ' . $col . ' from ' . $this->table . ' where ' . $where;
        parent::find($this->sql);
        if (empty($this->data) || $this->count() == 0) {
            return $this->data;
        } else
            return $this->data = $this->data[0];
    }

    public function findById($id = '1', $col = '*') {
        $this->sql = 'select ' . $col . ' from ' . $this->table . ' where id = "' . $id . '"';
        parent::find($this->sql);
        if (empty($this->data) || $this->count() == 0) {
            return $this->data;
        } else
            return $this->data = $this->data[0];
    }

    public function findByIds($id = array(), $col = '*') {
        $this->sql = 'select ' . $col . ' from ' . $this->table . ' where id in (' . implode(",", $id) . ')';
        parent::find($this->sql);
        if (empty($this->data) || $this->count() == 0) {
            return $this->data;
        } else
            return $this->data = $this->data;
    }

    public function delete($where = '1 = 1') {
        $this->sql = "delete from " . $this->table . " where " . $where;
        $this->data = null;
        return parent::query($this->sql);
    }

    public function update($where = '1 = 1', $params = array()) {
        if (empty($params) || count($params) == 0) {
            return 0;
        } else {
            $this->sql = "update " . $this->table . " set " . $this->genParams($params, ",") . " where " . $where;
            $this->data = null;
            return parent::query($this->sql);
        }
    }

    public function insert($params = array()) {
        if (empty($params) || count($params) == 0) {
            return 0;
        } else {
            $this->sql = "insert into " . $this->table . "(`" . implode("`,`", array_keys($params)) . "`) values ('" . implode("','", array_values($params)) . "')";
            parent::query($this->sql);
            $this->data = null;
            return parent::getId();
        }
    }

    public function setTable($table) {
        $this->table = $table;
    }

    private function genParams($params = array(), $char = ",") {
        $string = array();
        foreach ($params as $key => $val) {
            $string[] = $key . ' = "' . $val . '"';
        }
        return implode($char, $string);
    }

}
